package org.example.day20240827;

import java.util.*;

public class Poker {
    private Card[] cards = new Card[52];

    private String[] suits = {"红桃","黑桃","方片","草花"};
    public Poker() {
        int index = 0;
        for (int i = 0; i<suits.length; i++){
            for (int j = 1; j<=13; j++){
                cards[index++] = new Card(suits[i],j);
            }
        }
    }
    public void shuffle(){
        Random random = new Random();
        for(int i=0; i<52; i++){
            int num = random.nextInt(52);
            Card newCard = cards[num];
            cards[num] = cards[i];
            cards[i] = newCard;
        }
    }
    public List<Card> getOneHand(){
        this.shuffle();
        List<Card> result = new ArrayList<>();
        for (int i=0; i<5; i++){
            result.add(cards[i]);
        }
        return result;
    }
    public Card getOneCard(int index){
        return cards[index];
    }
    public String getType(List<Card> hand){
        Set<String> suitSet = new HashSet<>();
        List<Integer> rankList = new ArrayList<>();
        Set<Integer> rankSet = new HashSet<>();
        Map<Integer,Integer> rankCountMap = new HashMap<>();
        for (Card card : hand) {
            suitSet.add(card.getSuits());
            rankList.add(card.getRank());
            rankSet.add(card.getRank());
            if(rankCountMap.containsKey(card.getRank())){
                int count = rankCountMap.get(card.getRank());
                count++;
                rankCountMap.put(card.getRank(),count);
            }else {
                rankCountMap.put(card.getRank(),1);
            }
        }

        Collections.sort(rankList);

        boolean bIsStraight = false;
        boolean bIsSameColor = false;
        if(rankList.get(4) - rankList.get(0) == 4 && rankSet.size() == 5){
            bIsStraight = true;
        }
        if(suitSet.size() == 1){
            bIsSameColor = true;
        }
        if(bIsStraight && bIsSameColor){
            return "同花顺";
        }
        if(bIsStraight){
            return "顺子";
        }
        if(bIsSameColor){
            return "同花";
        }
        if(rankSet.size() == 4){
            return "一对";
        }
        if(rankSet.size() == 5){
            return "杂牌";
        }
        if(rankSet.size() == 2){
            if(rankCountMap.containsValue(4)){
                return "四条";
            }else {
                return "满堂红（3带2）";
            }
        }
        if(rankSet.size() == 3){
            if(rankCountMap.containsValue(3)){
                return "三条";
            }else {
                return "两对";
            }
        }
        return "unknow";
    }
    public Card[] getCards() {
        return cards;
    }

    public void setCards(Card[] cards) {
        this.cards = cards;
    }

    @Override
    public String toString() {
        String result = "";
        int index = 0;
        for (int i = 0; i<4; i++){
            for (int j = 1; j<=13; j++){
                result += cards[index++] +" ";
            }
            result += "\n";
        }
        return result ;
    }

    public static void main(String[] args) {
        Poker poker = new Poker();
        poker.shuffle();
        System.out.println(poker);
        List<Card> oneHand = poker.getOneHand();
        for(Card card : oneHand){
            System.out.print(card+" ");
        }
        System.out.println(poker.getType(poker.getOneHand()));
    }
}
